import constant from '@/store/constant.js'

export default {
	
	getVersion(type){
		var versionData=0;
		var dataString=uni.getStorageSync('versionData');
		if(dataString){
			var data=JSON.parse(dataString);
			switch(type){
				case 1:
					versionData=data.shareVersion;
					break;
				case 2:
					versionData=data.todoVersion;
					break;
				case 3:
					versionData=data.targetVersion;
					break;
				case 4:
					versionData=data.templateVersion;
					break;
				case 5:
					versionData=data.templateRecirdVersion;
					break;
			}
		}
		
		return versionData;
	},
	setVersion(type,value){
		var versionData=0;
		var dataString=uni.getStorageSync('versionData');
		var data;
		if(dataString){
			data=JSON.parse(dataString);
		}else{
			data ={
				shareVersion:0,
				todoVersion:0,
				targetVersion:0,
				templateVersion:0,
				templateRecirdVersion:0
			}
			
		}
		switch(type){
			case 1:
				data.shareVersion=value;
				break;
			case 2:
				
				data.todoVersion=value;
				break;
			case 3:
				
				data.targetVersion=value;
				break;
			case 4:
				
				data.templateVersion=value;
				break;
			case 5:
			
				data.templateRecirdVersion=value;
				break;
		}
		dataString=JSON.stringify(data);
		uni.setStorageSync('versionData',dataString);
	},
	removeSynchronousStatus(type,subType){
		console.log("removeSynchronousStatus")
		var cacheKey=""+type+":"+subType;
		var dataString=uni.getStorageSync('synchronousStatus');
		console.log(dataString)
		if(dataString){
			
			var data=JSON.parse(dataString);
			if(undefined!=data[cacheKey]){
				console.log(cacheKey+":"+data[cacheKey])
				delete data[cacheKey];
				
				dataString=JSON.stringify(data);
				console.log(dataString)
				uni.setStorageSync('synchronousStatus',dataString);
			}
		}
	},
	synchronousStatus(type,subType){
		var cacheKey=""+type+":"+subType;
		
		var status=false;
		var dataString=uni.getStorageSync('synchronousStatus');
		var data;
		let currentDate = new Date();
		let timestamp = currentDate.getTime();  
		let gapTime=3600*1000;
		if(dataString){
			data=JSON.parse(dataString);
			if(undefined!=data[cacheKey]){
				var cacheTime=data[cacheKey];
				if((timestamp-cacheTime)>gapTime){
					status=true;
					data[cacheKey]=timestamp;
				}
			}else{
				status=true;
				data[cacheKey]=timestamp;
			}
			
			
		}else{
						
			data ={}
			data[cacheKey]=timestamp;
			status=true;
		}
		if(status){
			dataString=JSON.stringify(data);
			uni.setStorageSync('synchronousStatus',dataString);
		}
		
		return status;
	},
	updateFieldvalue(type,subType,itemId,field,step,suffix){
		var cacheKey="data:"+type+":"+subType;
		var dataString=uni.getStorageSync(cacheKey);
		if(dataString){
			var data=JSON.parse(dataString);
			if(undefined!=data[itemId]&&undefined!=data[itemId][field]){
				var text=data[itemId][field];
				var subText=parseInt(text);
			
				var subText=subText+step;
			
				text=subText+suffix;
				data[itemId][field]=text;
				
				dataString=JSON.stringify(data);
			
				uni.setStorageSync(cacheKey,dataString);
			}
		}
	},
	setCacheData(type,subType,data){
		
		var cacheKey="data:"+type+":"+subType;
		var cacheOrderKey="dataOrder:"+type+":"+subType;
		
		var dataString=uni.getStorageSync(cacheKey);
		var dataOrderString=uni.getStorageSync(cacheOrderKey);
		
		var saveData;
		
		if(dataString){
			saveData=JSON.parse(dataString);
			
		}else{
			var saveData={};

		}
		for(var i=0;i<data.length;i++){
			saveData[data[i].id]=data[i];
		}
		var saveDataOrderArray=[];
		for(var key in saveData){
			saveDataOrderArray.push(saveData[key]);
		}
		saveDataOrderArray.sort((a,b)=>{
			let adate = new Date(a.createTime); 
			let aTimestamp = adate.getTime();  
			let bdate = new Date(b.createTime);
			let bTimestamp = bdate.getTime();  
			return bTimestamp-aTimestamp;
		});
		var saveDataOrder=[];
		for(var i=0;i< saveDataOrderArray.length;i++ ){
			saveDataOrder.push(saveDataOrderArray[i].id);
		}
		
		
		dataString=JSON.stringify(saveData);
		uni.setStorageSync(cacheKey,dataString);
		
		uni.setStorageSync(cacheOrderKey,JSON.stringify(saveDataOrder));
	},
	getCacheData(type,subType){
		var cacheKey="data:"+type+":"+subType
		var cacheOrderKey="dataOrder:"+type+":"+subType;
		var dataString=uni.getStorageSync(cacheKey);
		console.log(dataString)
		var result=[];
		if(dataString){
			
			var dataOrderString=uni.getStorageSync(cacheOrderKey);
			var data=JSON.parse(dataString);
			var dataOrder=JSON.parse(dataOrderString);
			for(var i=0;i<dataOrder.length;i++){
				result.push(data[dataOrder[i]]);
			}
			
			
		}
		return result;
	},
	getCacheItemData(type,subType,itemId){
		var cacheKey="data:"+type+":"+subType
		var cacheOrderKey="dataOrder:"+type+":"+subType;
		var dataString=uni.getStorageSync(cacheKey);
		
		var result=null;
		if(dataString){
			var data=JSON.parse(dataString);
			if(undefined!=data[itemId]){
				result=data[itemId]
			}
			
			
			
			
		}
		return result;
	},
	deleteCacheItemData(type,subType,itemId){
		var cacheKey="data:"+type+":"+subType
		var cacheOrderKey="dataOrder:"+type+":"+subType;
		var dataString=uni.getStorageSync(cacheKey);
		
		var result=null;
		if(dataString){
			
			var dataOrderString=uni.getStorageSync(cacheOrderKey);
			var data=JSON.parse(dataString);
			var dataOrder=JSON.parse(dataOrderString);
			if(undefined!=data[itemId]){
				delete data[itemId]
			}
			var tempList=[];
			if(dataOrder.length>0){
				var index=0;
				for(var i=0;i<dataOrder.length;i++){
					if(dataOrder[i]!=itemId){
						tempList.push(dataOrder[i]);
					}
				}
				dataOrder=tempList
				
			}
			dataString=JSON.stringify(data);
			uni.setStorageSync(cacheKey,dataString);
			
			uni.setStorageSync(cacheOrderKey,JSON.stringify(dataOrder));
		}
		
	},
	getCacheDataForPage(type,subType,searchValue,page,size){
		var cacheKey="data:"+type+":"+subType
		var cacheOrderKey="dataOrder:"+type+":"+subType;
		console.log(cacheKey)
		var dataString=uni.getStorageSync(cacheKey);
		console.log(dataString)
		var result=[];
		if(dataString){
			var start =(page-1)*size;
			var end=page*size;
			console.log("---"+start)
			console.log("---"+end)
			var dataOrderString=uni.getStorageSync(cacheOrderKey);
			console.log(dataOrderString);
			var data=JSON.parse(dataString);
			var dataOrder=JSON.parse(dataOrderString);
			var list;
			if(searchValue.length>0){
				
				
				var orderList=[];
				console.log("searchValue:"+searchValue)
				list={};
				console.log(data)
				for(var i in data){
					
					
					for(var j=0;j<data[i].value.length;j++){
						var item =data[i].value[j];
						switch(item.fieldType){
							case 5:
							case 6:
								break;
							default:
								
								
								if("string"==typeof item.value){
									var index = item.value.indexOf(searchValue);
									if(index>=0){
										console.log(item.value);
										list[i]=data[i];
										orderList.push(data[i].id);
										
										
									}
								}
									
								
								break;
						}
					}
					
					
					
				}
				
				var tempList=[];
				if(orderList.length>0){
					console.log("orderList")
					console.log(dataOrder)
					console.log(orderList)
					for(var i=0;i<dataOrder.length;i++){
						
						for(var j=0;j<orderList.length;j++){
							if(dataOrder[i]==orderList[j]){
								tempList.push(dataOrder[i]);
							}
						}
					}
					
					
					dataOrder=tempList;
				}else{
					dataOrder=[]
				}
				
				
			}else{
				list=data;
			}
			
			if(dataOrder.length>0){
				for(var i=start;i<end;i++){
					if(list[dataOrder[i]]!=undefined){
						
						result.push(list[dataOrder[i]]);
					}
					
				}
			}
			
			
			
		}
		return result;
	},
	jumpToLogin(){
		console.log("jumpToLogin")
		var url="/pages/login/login";
		uni.removeStorageSync('token');
		uni.removeStorageSync('userInfo');
		uni.showToast({
		  title: "服务器异常，3秒之后自动跳转到登陆",
		  icon: "error"
		})
		setTimeout(() => {
			uni.navigateTo({
				url:url
			})
		}, 3000);
		
		
	},
	dataError(data){
		uni.showToast({
		  title:data.message,
		  icon: "error"
		})
	}

}